<?php

class GroupMembers extends \Phalcon\Mvc\Model
{

    /**
     *
     * @var integer
     */
    public $id;

    /**
     *
     * @var integer
     */
    public $group_id;

    /**
     *
     * @var integer
     */
    public $user_id;

    /**
     *
     * @var integer
     */
    public $date_created;

    /**
     * Independent Column Mapping.
     */
    public function columnMap()
    {
        return array(
            'id' => 'id', 
            'group_id' => 'group_id', 
            'user_id' => 'user_id', 
            'date_created' => 'date_created'
        );
    }

    public function checkIfMember($user_id)
    {   
        return GroupMembers::findFirst(array(
                    'conditions'=>'user_id = ?0',
                    'bind'=>array(0=>$user_id)
                ));
    }
    public function beforeValidationOnCreate()
    {
        $this->date_created = CURR_DATE;
    }
    public function addMember($group_id,$user_id){
        $member = new GroupMembers();
        $member->group_id = $group_id;
        $member->user_id = $user_id;
        return $member->save();
    }
    public function listMembers($group_id,$me){
        $phql = "SELECT gm.user_id,uat.fname,uat.lname,uat.profile_pic FROM GroupMembers gm 
                 LEFT JOIN UserAccountTb uat ON uat.id = gm.user_id
                 WHERE gm.group_id = ?0 and gm.user_id != ?1
                 ORDER BY gm.date_created ASC";
        $data = $this->modelsManager->executeQuery($phql,array(0=>$group_id,1=>$me));
        return @$data;

    }
    public function countMembers($group_id)
    {
        $phql = "SELECT COUNT(gm.id) as total FROM GroupMembers gm 
                 WHERE gm.group_id = ?0";
        $data = $this->modelsManager->executeQuery($phql,array(0=>$group_id));
        return @$data[0]->total;        
    }

}
